<template>
  <div class="user-home-root-page">
    <div class="user-home-main">
      <div class="user-header">
        <div class="left-userinfo">
          <img
            class="user-head-picture"
            :src="userInfo.headPicture || defaultUserImg"
            alt=""
          />
          <div class="user-detail">
            <h3 class="username">{{ userInfo.username }}</h3>
            <p class="saying">{{ userInfo.saying }}</p>
          </div>
        </div>
        <div class="tags-wrap">
          <NavSwiper
            v-if="allTagList.length > 0"
            :data="allTagList"
            v-model="search.tagId"
            slideActiveClass="us-scroll-active-line-col"
            slideClass="us-scroll-line-col"
            @slideClick="slideClick"
          ></NavSwiper>
        </div>
      </div>

      <div class="essay-list-container">
        <div
          v-for="(essay, index) in essayList"
          :key="index"
          class="essay-item"
        >
          <div class="essay-title-cell"  @click="linkEssay(essay)">
            <span class="essay-title">  {{ essay.essaytitle }}</span>
          
            <img v-if="essay.status==1" class="top" src="~src/assets/top.png" alt="">
          </div>
          <div class="content-cell">
            <div class="thumbnail" v-if="transformImg(essay.content)">
              <img :src="transformImg(essay.content)" alt="" />
            </div>
            <div
              class="essay-content"
              v-html="transformContent(essay.content)"
            ></div>
          </div>
          <div class="essay-content-footer">

            <div class="footer-left">
              <div class="update-time">
              {{ essay.updated_at ? essay.updated_at : essay.created_at }}
            </div>
            <div class="view-count">
              <img src="~src/assets/eye.png" alt="" />
              <span> {{ essay.view_count }}</span>
            </div>
            </div>
            <div class="tagsshow">
              {{ essay.tags.map((tag) => tag.tagname).join("、") }}
            </div>
          </div>
        </div>

        <div class="load-more-cell">
          <div
            class="mr-bottom"
            v-if="essayList.length > 0 && essayList.length < pagination.total"
            @click="loadMore"
          >
            点击加载更多
          </div>
          <div class="mr-bottom" v-else>没有更多了</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, ref, computed } from "vue";
import request from "utils/request";
import { getParamsCode } from "utils/getParamsCode";
import NavSwiper from "src/components/NavSwiper/NavSwiper";

export default {
  name: "NestVueShortPassageHome",
  components: { NavSwiper },
  setup() {
    const id = getParamsCode("id") || '21acc217-0e4f-4a92-8808-5f4b9ae9d7e1';
    const search = reactive({
      essaytitle: "",
      tagId: "",
      userId: id,
    });
    const essayList = ref([]);
    const allTagList = ref([]);
    const userInfo = ref({});
    const pagination = reactive({
      currentPage: "1",
      pageSize: "10",
      total: 0,
    });

    const getUserInfo = async () => {
      let ret = await request.get(`/user/${id}`);

      if (ret.code == 200) {
        userInfo.value = ret.data;
        console.log(ret.data);
      }
    };

    const getAllTagList = async () => {
      let ret = await request.get("tag/list", {
        tagname: "",
        userId: search.userId,
      });
      if (ret.code == 200) {
        allTagList.value = ret.data[0] || [];
        allTagList.value.unshift({
          id: "",
          tagname: "全部",
        });
      }
    };

    const getEssayList = async (init) => {
      let ret = await request.get("/essay/list", {
        essaytitle: search.essaytitle,
        tagId: search.tagId,
        userId: search.userId,
        currentPage:pagination.currentPage,
        pageSize: pagination.pageSize,
      });
      if (ret.code == 200) {
        if (ret.data[0] && ret.data[0].length > 0) {
          init
            ? (essayList.value = ret.data[0])
            : essayList.value.push(...ret.data[0]);
          console.log("this.essayList", essayList.value);
          pagination.total = ret.data[1];
        } else {
          essayList.value = [];
        }
      }
    };

    const slideClick = (slide) => {
      search.tagId = slide.id;
      pagination.currentPage=1
      getEssayList(true);
    };

    const linkEssay = (essay) => {
      window.open(
        "/blog/essay.html?id=" + essay.id,
        "__blank"
      );
    };

    const transformImg = (content) => {
      let imgReg = /<img.*?(?:>|\/>)/gi; //匹配图片中的img标签
      let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; // 匹配图片中的src
      let arr = content.match(imgReg); //筛选出所有的img
      let srcArr = [];
      if (arr && arr.length) {
        for (let i = 0; i < arr.length; i++) {
          let src = arr[i].match(srcReg);
          // 获取图片地址
          srcArr.push(src[1]);
        }
      }
      return srcArr[0] ? srcArr[0] : undefined;
    };

    const transformContent = (content) => {
      var text = content.replace(/<img.*>.*<\/img>/gi, ""); //过滤如<img></img>形式的图片元素
      text = text.replace(/<img.*\/>/gi, ""); //过滤如<img />形式的元素
      text = text.replace(/\r|\n/gi, ""); //过滤换行符
      text = text.replace(/<[^>]+>/g, "").trim(); //过滤HTML标签
      return text;
    };

    const loadMore = () => {
      Object.assign(pagination, {
        currentPage: +pagination.currentPage + 1,
      });
      getEssayList();
    };

    const defaultUserImg = computed(() => {
      return require("src/assets/user-header.png");
    });

    onMounted(async () => {
      await getUserInfo();
      await getAllTagList();
      await getEssayList();
    });

    return {
      getEssayList,
      search,
      essayList,
      allTagList,
      pagination,
      linkEssay,
      userInfo,
      transformImg,
      transformContent,
      defaultUserImg,
      loadMore,
      slideClick,
    };
  },
};
</script>

<style lang="scss">
// .us-scroll-line-col:first-of-type {
//   margin-left: 20px;
// }
.us-scroll-line-col {
  cursor: pointer;
  position: relative;
  height: 32px !important;
  line-height: 32px;
  text-align: center;
  width: auto !important;
  flex: 1 0 auto;
  padding: 0 12px;
}
.us-scroll-active-line-col {
  color: #056de8;
}
.us-scroll-active-line-col:before {
  position: absolute;
  left: 50%;
  bottom: 0;
  content: "";
  z-index: 2;
  width: 20px;
  height: 2px;
  margin-left: -10px;
  background: #056de8;
  background-size: contain;
}
</style>

<style lang="scss" scoped>
.tags-wrap {
  padding: 20px 0px 0px;
}
.user-home-root-page {
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
  background: #f8f8f8;

  .user-header {
    // display: flex;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.08);
    transition: 0.3s;
    margin-bottom: 15px;
    padding: 50px 12px 20px;
    background: white;
    .crosswise-inner-wrap {
      display: flex;
      flex-wrap: nowrap;
      & > div {
        flex-shrink: 0;
      }
    }
    .left-userinfo {
      display: flex;
      align-items: flex-end;
      .user-head-picture {
        width: 120px;
        height: 120px;
        margin-right: 20px;
      }
      .user-detail {
        padding-bottom: 5px;
        .username {
          margin-bottom: 8px;
          font-size: 18px;
          font-family: cursive;
        }
        .saying {
          font-size: 13px;
          color: #8590a6;
        }
      }
    }
  }

  .user-home-main {
    width: 850px;
    margin: 0 auto;
    height: 100%;
    box-sizing: border-box;
    // background: white;
  }

  .essay-list-container {
    padding: 20px 10px 40px;
    min-height: 500px;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.08);
    background: white;
    .essay-item {
      margin-bottom: 25px;
      min-height: 80px;
      padding: 10px 15px;
      border-bottom: 1px solid #f0f0f2;
      cursor: pointer;
      .essay-title-cell{
        display: flex;
        justify-content: space-between;
        .essay-title {
        line-height: 28px;
        font-size: 20px;
        font-weight: 500;
        margin-bottom: 15px;
        color: #056de8;
        flex: 1;
      }
      
      .top{
         
          width: 28px;
          height: 28px;
          flex-shrink: 1;
        }
      }

      .content-cell {
        display: flex;
        width: 100%;
        max-height: 150px;
        overflow: hidden;
        .thumbnail {
          width: 30%;
          overflow: hidden;
          margin-right: 15px;
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        .essay-content {
          font-size: 14px;
          flex: 1;
          color: #555666;
          margin-top: 4px;
          line-height: 28px;
          max-height: 150px;
          overflow: hidden;
          white-space: normal;
          word-break: break-word;
          text-overflow: ellipsis;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }
        margin-bottom: 12px;
      }
      .essay-content-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .footer-left{
          display: flex;
          align-items: center;
          .update-time {
          font-size: 13px;
          line-height: 28px;
          color: #8590a6;
        }
        .view-count{
          line-height: 28px;
          padding-left: 8px;
          img{
            width: 15px;
            margin-right: 3px;
            //  transform: rotate(90deg);
          }
          font-size: 13px;
          display: flex;
          align-items: center;
          color: #8590a6;
        }
        }

        .tagsshow {
          max-width: 400px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          color: #8590a6;
          font-size: 13px;
        }
      }
    }
    .load-more-cell {
      text-align: center;
      line-height: 32px;
      color: #8590a6;
      cursor: pointer;
      font-size: 13px;
    }
  }

  @media screen and (max-width: 850px) {
    .user-home-main {
      width: 100%;
    }
    .tagsshow {
      max-width: 120px !important;
    }
  }
}
</style>
